﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDTB.Common;
using LDTB.Common.Entities;

namespace LDTB.UI
{
    public partial class EditVillage : UIBase
    {
        int id = -1;
        public int IDVillage
        {
            get
            {
                if (Request["ID"] != null)
                {
                    if (!int.TryParse(Request["ID"].ToString(), out id))
                    {
                        id = -1;
                    }
                }
                return id;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadWard();
                if (IDVillage != -1)
                {
                    LoadVillage();
                    Button1.Text = "Lưu lại";
                }
                else
                {
                    Button1.Text = "Thêm mới";
                }
            }
        }

        private void LoadWard()
        {
            var wards = from w in Data.VC_Huyens
                        select new ListItem { Text = w.Name, Value = w.ID.ToString() };

            ddVillage.Items.Add(new ListItem { Value = "-1", Text = "----" });
            if (wards != null && wards.Count() > 0)
            {
                ddVillage.Items.AddRange(wards.ToArray());
            }

        }

        private void LoadVillage()
        {
            var vl = (from v in Data.VC_Xas
                      where v.ID == IDVillage
                      select v).FirstOrDefault();
            if (vl != null)
            {
                this.txt_Village.Text = vl.Name;

                for (int i = 0; i < ddVillage.Items.Count; i++)
                {

                    if (vl.HuyenID.Value.ToString() == ddVillage.Items[i].Value)
                    {
                        ddVillage.SelectedIndex = i;
                        break;
                    }
                }
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ddVillage.SelectedIndex == 0)
            {
                this.ddVillage.Style.Add("background-color", "red");
            }
            else
            {
                if (IDVillage != -1)
                {
                    var village = (from v in Data.VC_Xas
                                   where v.ID == IDVillage
                                   select v).FirstOrDefault();
                    if (village != null)
                    {
                        village.Name = txt_Village.Text.Trim();
                        village.HuyenID = int.Parse(ddVillage.SelectedValue);
                    }
                }
                else
                {
                    var vill = new VC_Xa { Name = txt_Village.Text.Trim(), HuyenID = int.Parse(ddVillage.SelectedValue) };
                    Data.VC_Xas.InsertOnSubmit(vill);
                }

                Data.SubmitChanges();
                Response.Redirect(EditUrl("Village"));
            }
        }
    }
}